<?php

//
// +------------------------------------------------------------------------+
// | PHP Version 5.4                                                        |
// +------------------------------------------------------------------------+
// | Copyright (c) All rights reserved.                                     |
// +------------------------------------------------------------------------+
// | 世纪车来车往(北京）网络科技有限公司 版权所有！                               |
// | TEL:010-63732365  HOTLINE:400-6688-365  EMAIL:clcw@clcw.com.cn         |
// | $Revision: 1.0 $                                                       |
// +------------------------------------------------------------------------+
// | Author: xiangminghu@clcw.com.cn                                        |
// +------------------------------------------------------------------------+
//

namespace AuctionBack\Controller;

use AuctionCore\Account\Back\Employee;
use AuctionCore\Other\Join as Join;
use AuctionCore\Common\Page;
use AuctionCore\System\AuctionException;

/**
 * 免费加盟
 * @author Xmh
 */
class JoinController extends BaseController {

    public function __construct() {
        parent::__construct();
        //页面头部信息
        $head = C('HEAD_INDEX');
        $this->assign("web_title", $head['title']);
        $this->assign("web_keywords", $head['keywords']);
        $this->assign("web_description", $head['description']);
        $this->emp_id = $this->user_info['emp_id'];

        $this->Join = new Join();
        //模板赋值
        $this->assign("emp_id", $this->emp_id);
    }

    public function index() {
        $this->display();
    }

    public function track() {
        $this->display();
    }

    /**
     * 加盟列表
     */
    public function join_list() {
        $page = I("get.p", 1);
        $size = 10;
        $params = array();
        $params['book_no'] = I('get.book_no');
        $params['id'] = I('get.id/d',0);
        $params['phone'] = I('get.phone','','trim');
        $params['comefrom'] = I('get.comfrom');
        $params['start_time'] = I('get.start_time');
        $params['end_time'] = I('get.end_time');
        $params['dealer_name'] = I('get.dealer_name','','trim');
        $params['status'] = I('get.status');
        $params['emp'] = I('get.emp','','trim');
        $params['emp_id'] = $this->emp_id;
        //获取list
        $result = $this->Join->get_join_list($page, $size, $params);
        $list = empty($result['list']) ? array() : $result['list'];
        //分页
        $Page = new \AuctionCore\Common\AjaxPage($result['total'], $size, "get_page");
        $Page->setConfig('theme', " %totalRow% %header% %nowPage%/%totalPage% 页 %first% %upPage% %prePage% %linkPage% %nextPage% %downPage%  %end% %jumpform%");
//        dump($list);
        $show = $Page->show();          // 分页显示输出
        $this->assign('page', $show);   // 赋值分页输出
        $this->assign('list', $list);
        $this->assign('p', $page);
        $this->assign('status', $params['status']);
        $this->assign('total', $result['total']);
        $this->display();
    }

    /**
     * 处理加盟页面
     */
    public function join_deal() {
        $id = I('get.id/d');
        if ($id < 1) {
            $this->helpJsonResult(400, "加盟信息不存在");
        }
        $join = $this->Join->get_join_info($id);
        $join_log = $this->Join->get_join_log($id);
        $trace_log =  array_filter(explode("/n/t", $join_log['action_log']));
        foreach($trace_log as $k=>$v){
            $join_trace_log[$k] = explode('/t', $v);
        }
        $this->assign('join', $join);
        $this->assign('join_trace_log',$join_trace_log);
        $this->display();
    }

    /**
     * 处理加盟
     */
    public function do_join_deal() {
        $id = I('post.id/d');
        if ($id < 1) {
            $this->helpJsonResult(400, "加盟信息不存在");
        }
        $remark = I('post.remark');
        $status = I('post.status');
        if ($status == 2) {
            $next_trace_time = I('post.next_trace_time');
            if(!validateDatetime($next_trace_time)){
                $this->helpJsonResult(400, "请选择跟踪时间");
            }
            $data['next_trace_time'] = $next_trace_time;
            $join_trace_msg = '继续跟踪';
        } elseif ($status == 4) {
            $join_trace_msg = '处理失败';
        }
        if ($status == 3) {
            $dealer_name = I('post.dealer_name', '', 'trim');
            if (empty($dealer_name)) {
                $this->helpJsonResult(400, "请填写车商姓名");
            }
//            $dealer = new \AuctionCore\CarDealer\Dealer();
//
//            $dealer_id = $dealer->get_dealer_id_by_dealer_name($dealer_name);
//            if(empty($dealer_id)){
//                $this->helpJsonResult(400, "车商不存在");
//            }
            $data['username'] = $dealer_name;
            $join_trace_msg = '处理成功';
        }
        if (empty($remark)) {
            $this->helpJsonResult(400, "请填写备注");
        }
        $data['id'] = $id;
        $data['remark'] = $remark;
        $data['status'] = $status;
        $data['emp_id'] = $this->emp_id;
        $res = $this->Join->update_join_post($data);
        if ($res === false) {
            $this->helpJsonResult(500, "处理失败");
        }
        $employee = Employee::user_info_id($this->emp_id);
        $join_log = array(
            'join_id' => $id,
            'join_trace_msg' => $join_trace_msg,
            'createtime' => date("Y-m-d H:i:s"),
            'employee' => $employee['real_name'],
            'remark' => $remark
        );
        $this->Join->add_join_log($join_log);
        $this->helpJsonResult(200, "处理成功");
    }

    /**
     * 查看加盟
     */
    public function join_check($id) {
        $id = I('get.id/d');
        if ($id < 1) {
            $this->helpJsonResult(400, "加盟信息不存在");
        }
        $join = $this->Join->get_join_info($id);
        $join_log = $this->Join->get_join_log($id);
        $trace_log =  array_filter(explode("/n/t", $join_log['action_log']));
        foreach($trace_log as $k=>$v){
            $join_trace_log[$k] = explode('/t', $v);
        }
        $this->assign('join', $join);
        $this->assign('join_trace_log',$join_trace_log);
        $status = $join['status'];
        if ($status < 3) {
            $this->helpJsonResult(400, "加盟信息未处理完成");
        }
        $this->assign('join', $join);
        $this->assign('join_trace_log',$join_trace_log);
        $this->display();
    }

}
